/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include <stdafx.h>

#include "系统平台.h"

#include <Windows.h>
#include <字符串转换.h>




std::wstring f_sys_get剪切板文本() {
	std::wstring text;

	if (OpenClipboard(nullptr)) {
		if (IsClipboardFormatAvailable(CF_TEXT)) {

			auto 剪切板 = GetClipboardData(CF_TEXT);
			auto pBuf = (int8*)GlobalLock(剪切板);
			std::string t = pBuf;
			GlobalUnlock(剪切板);

			text = f_StringToWString(t, "gbk");

			CloseClipboard();
		}
	}

	return text;
}

void f_sys_写到剪切板(const std::string& data) {
	uint64 dwLength = data.size();	
	HANDLE hGlobalMemory = GlobalAlloc(GHND, dwLength + 1);	
	LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory);	


	auto 转码 = f_String转码(data, "gbk");
	auto* pData = (uint8*)转码.data();
	//auto* pData = data.data();
	for (int32 i = 0; i < dwLength; ++i) *lpGlobalMemory++ = pData[i];

	GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
	//HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄

	

	OpenClipboard(nullptr); // 打开剪贴板
	EmptyClipboard(); // 清空剪贴板
	SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
	CloseClipboard(); // 关闭剪贴板
}



